//
//  Status.swift
//  SinaWeibo
//
//  Created by NeXTSTEP on 15/10/1.
//  Copyright © 2015年 NeXTSTEP. All rights reserved.
//

import UIKit
import SDWebImage


class Status: NSObject {
    var text: String?
    var source: String?
    var created_at: String?
    var id: Int = 0
    
    var pic_urls:[[String : String]]?{
        didSet{
            if let urls = pic_urls {
                imageURLS = [NSURL]()
                largeImageURLs = [NSURL]()
                for dict in urls {
                    if let urlString = dict["thumbnail_pic"] {
                        let url = NSURL(string: urlString)!
                        imageURLS!.append(url)
                        
                        let largeURLString = urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "large")
                        let largeURL = NSURL(string: largeURLString)!
                        largeImageURLs?.append(largeURL)
                    }
                }
            }
        }
    }
    
    var imageURLS: [NSURL]?
    var largeImageURLs:[NSURL]?
    
    var pictureURLS: [NSURL]? {
        if retweeted_status != nil {
            return retweeted_status?.imageURLS
        }
        return imageURLS
    }
    
    var largePictureURLs:[NSURL]?{
        return retweeted_status == nil ? largeImageURLs : retweeted_status?.largeImageURLs
    }
    
    var retweeted_status: Status?
    
    var user: User?
    
    class func loadStatus(completion:(statuses: [Status]?) -> ()) {
        NetworkTools.sharedTools.loadStatus { (dict, error) -> () in
            if error != nil {
                completion(statuses: nil)
                return
            }
            
            if let statuses = dict!["statuses"] as? [[String : AnyObject]] {
                let list = Status.statuses(statuses)
                Status.cacheStatusImage(list, completion: completion)
            } 
        }
    }
    
    class func cacheStatusImage(array: [Status]?,completion:(statuses: [Status]?) -> ()) {
        if array == nil {
            completion(statuses: nil)
            return
        }
        
        let group = dispatch_group_create()
        for s in array as [Status]! {
            if s.pictureURLS == nil {
                continue
            }
            for url in s.pictureURLS! {
                dispatch_group_enter(group)
                SDWebImageManager.sharedManager().downloadImageWithURL(url, options: SDWebImageOptions.RetryFailed, progress: nil, completed: { (_, _, _, _, _) -> Void in
                    dispatch_group_leave(group)
                })
            }
        }
        
        dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
            print("完成所有的图片下载")
            completion(statuses: array)
        }
    }
    
    class func statuses(array: [[String : AnyObject]]) -> [Status]? {
        if array.count == 0 {
            
            return nil
        }
        
        var list = [Status]()
        for item in array {
            list.append(Status(dict: item))
        }
        return list
    }
    
    
    init(dict: [String : AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
        if dict["user"] != nil{
            user = User(dict: dict["user"] as! [String : AnyObject])
        }
        
        if dict["retweeted_status"] != nil {
            retweeted_status = Status(dict: dict["retweeted_status"] as! [String: AnyObject])
        }
        
    }
    
//    override func setValue(value: AnyObject?, forKey key: String) {
//        if key == "user" {
//            if let dict = value as? [String : AnyObject] {
//                user = User(dict: dict)
//            }
//            return
//        }
//        
//        super.setValue(value, forKey: key)
//        
//    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {    }
    
    
}
